/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */



package sonia.golf;

//~--- non-JDK imports --------------------------------------------------------

import android.app.Activity;
import android.app.ListActivity;

import android.os.Bundle;

import android.util.Log;

import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;

import android.widget.AdapterView;
import android.widget.Toast;

import sonia.golf.dao.CourseDAO;
import sonia.golf.dao.DAOFactory;
import sonia.golf.view.DoubleAdapter;

//~--- JDK imports ------------------------------------------------------------

import java.util.List;

/**
 *
 * @author Sebastian Sdorra
 */
public class CourseDetailActivity extends ListActivity
{

  /** Field description */
  public static final int CONTEXTMENU_EDIT = 0;

  /** Field description */
  public static final int CONTEXTMENU_REMOVE = 1;

  //~--- methods --------------------------------------------------------------

  /**
   * Method description
   *
   *
   * @param item
   *
   * @return
   */
  @Override
  public boolean onContextItemSelected(MenuItem item)
  {
    String message = null;

    switch (item.getItemId())
    {
      case CONTEXTMENU_EDIT :
        message = "EDIT";

        break;

      case CONTEXTMENU_REMOVE :
        message = "REMOVE";

        break;
    }

    if (message != null)
    {
      Hole hole = getHole(item.getMenuInfo());

      message = message.concat(" HOLE ").concat(hole.toString());
    }
    else
    {
      message = "NOT FOUND";
    }

    Toast.makeText(this, message, Toast.LENGTH_LONG).show();

    return true;
  }

  /**
   * Method description
   *
   *
   * @param menu
   * @param v
   * @param menuInfo
   */
  @Override
  public void onCreateContextMenu(ContextMenu menu, View v,
                                  ContextMenuInfo menuInfo)
  {
    super.onCreateContextMenu(menu, v, menuInfo);
    menu.setHeaderTitle(R.string.title_hole_ctx_menu);
    menu.add(0, CONTEXTMENU_EDIT, 0, R.string.edit);

    Hole hole = getHole(menuInfo);

    if ((hole != null) && (hole.getNumber() == holeList.size()))
    {
      menu.add(0, CONTEXTMENU_REMOVE, 0, R.string.remove);
    }
  }

  /**
   * Method description
   *
   *
   * @param menu
   *
   * @return
   */
  @Override
  public boolean onCreateOptionsMenu(Menu menu)
  {
    MenuInflater mi = getMenuInflater();

    mi.inflate(R.layout.menucoursedetail, menu);

    return true;
  }

  /**
   * Method description
   *
   *
   * @param item
   *
   * @return
   */
  @Override
  public boolean onOptionsItemSelected(MenuItem item)
  {
    switch (item.getItemId())
    {
      case R.id.menu_new_hole :
        short number = 1;

        if (!holeList.isEmpty())
        {
          number = (short) (holeList.size() + 1);
        }

        holeList.add(new Hole(number, (short) 3));
        adapter.notifyDataSetChanged();

        break;

      case R.id.menu_quit :
        System.exit(RESULT_OK);
    }

    return true;
  }

  /**
   * Method description
   *
   *
   * @param savedInstanceState
   */
  @Override
  protected void onCreate(Bundle savedInstanceState)
  {
    super.onCreate(savedInstanceState);

    String courseName =
      getIntent().getExtras().getString(StrokeCounter.PARAMETER_COURSE);

    setTitle(courseName);

    CourseDAO courseDAO = DAOFactory.getInstance().getCourseDAO();

    course = courseDAO.get(courseName);
    holeList = course.getHoles();

    if (holeList.isEmpty())
    {

      // create basic data
      holeList.add(new Hole((short) 1, (short) 3));
      holeList.add(new Hole((short) 2, (short) 3));
      holeList.add(new Hole((short) 3, (short) 3));
    }

    adapter = new HoleAdapter(this, holeList);
    registerForContextMenu(getListView());
    setListAdapter(adapter);
  }

  /**
   * Method description
   *
   *
   * @param hole
   */
  private void remove(Hole hole) {}

  /**
   * Method description
   *
   *
   * @param hole
   */
  private void showEditDialog(Hole hole) {}

  //~--- get methods ----------------------------------------------------------

  /**
   * Method description
   *
   *
   * @param mennuInfo
   *
   * @return
   */
  private Hole getHole(ContextMenuInfo mennuInfo)
  {
    AdapterView.AdapterContextMenuInfo info = null;

    try
    {
      info = (AdapterView.AdapterContextMenuInfo) mennuInfo;
    }
    catch (ClassCastException e)
    {
      Log.e(StrokeCounter.LOG_TAG, "bad menuInfo", e);
    }

    return (info != null)
           ? adapter.getItem(info.position)
           : null;
  }

  //~--- inner classes --------------------------------------------------------

  /**
   * Class description
   *
   *
   * @version        Enter version here..., 10/09/01
   * @author         Enter your name here...
   */
  private static class HoleAdapter extends DoubleAdapter<Hole>
  {

    /**
     * Constructs ...
     *
     *
     * @param activity
     * @param itemList
     */
    public HoleAdapter(Activity activity, List<Hole> itemList)
    {
      super(activity, itemList);
    }

    //~--- get methods --------------------------------------------------------

    /**
     * Method description
     *
     *
     * @param item
     *
     * @return
     */
    @Override
    protected String getLeftTextFromItem(Hole item)
    {
      return String.valueOf(item.getNumber());
    }

    /**
     * Method description
     *
     *
     * @param item
     *
     * @return
     */
    @Override
    protected String getRigthTextFromItem(Hole item)
    {
      return String.valueOf(item.getPar());
    }
  }


  //~--- fields ---------------------------------------------------------------

  /** Field description */
  private HoleAdapter adapter;

  /** Field description */
  private Course course;

  /** Field description */
  private List<Hole> holeList;
}
